package ui;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.sql.SQLException;

import javax.swing.AbstractAction;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.LayoutStyle;
import tag.tag;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;

import dao.BirdDAO;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class tagCloudWindow extends javax.swing.JFrame {
	private JComboBox tagCloudList;
	private JLabel tagLabel;
	private AbstractAction generate;
	private JButton generateButton;
	private static String[] listaOMG = new String[] { "Color" , "Habitat" , "Country" , "Diet" , 
		"Risk of Extinction" , "Scientific Name" };

	/**
	* Auto-generated main method to display this JFrame
	*/
		
	public tagCloudWindow() {
		super();
		initGUI();
		
		this.setResizable(false);
		this.setTitle("Tag Cloud");
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			GroupLayout thisLayout = new GroupLayout((JComponent)getContentPane());
			getContentPane().setLayout(thisLayout);
			this.setName("Tag Cloud");
			{
				ComboBoxModel tagCloudListModel = 
					new DefaultComboBoxModel(
							listaOMG );
				tagCloudList = new JComboBox();
				tagCloudList.setModel(tagCloudListModel);
			}
			{
				generateButton = new JButton();
				generateButton.setText("Generate Tag Cloud");
				generateButton.setAction(getGenerate());
			}
			{
				tagLabel = new JLabel();
				tagLabel.setText("Select the desired attribute to generate the tag cloud:");
			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addComponent(tagLabel, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				.addComponent(tagCloudList, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				.addComponent(generateButton, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addGroup(thisLayout.createParallelGroup()
				    .addComponent(tagLabel, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 314, GroupLayout.PREFERRED_SIZE)
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addComponent(tagCloudList, GroupLayout.PREFERRED_SIZE, 176, GroupLayout.PREFERRED_SIZE)
				        .addGap(0, 138, Short.MAX_VALUE))
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addComponent(generateButton, GroupLayout.PREFERRED_SIZE, 176, GroupLayout.PREFERRED_SIZE)
				        .addGap(0, 138, Short.MAX_VALUE))));
			pack();
			this.setSize(328, 148);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
		
	}
	
	private AbstractAction getGenerate() {
		if(generate == null) {
			generate = new AbstractAction("Generate Tag Cloud", null) {
				public void actionPerformed(ActionEvent evt) {
					String gen = "";
					String selected = tagCloudList.getSelectedItem().toString(); 
					
					//color
					try {
					if ( selected.equals(listaOMG[0]) )
						gen = tag.CreateColorCloud(BirdDAO.arbitrarySQL("select * from bird"));
					else if ( selected.equals(listaOMG[1]) ) //habitat
						gen = tag.CreateHabitatCloud(BirdDAO.arbitrarySQL("select * from bird"));
					else if ( selected.equals(listaOMG[2]) ) //country
						gen = tag.CreateCountryCloud(BirdDAO.arbitrarySQL("select * from bird"));
					else if ( selected.equals(listaOMG[3]) ) //diet
						gen = tag.CreateDietCloud(BirdDAO.arbitrarySQL("select * from bird"));
					else if ( selected.equals(listaOMG[4]) ) //risk
						gen = tag.CreateRiskOfExtinctionCloud(BirdDAO.arbitrarySQL("select * from bird"));
					else if ( selected.equals(listaOMG[5]) ) //sciname
						gen = tag.CreateScientificNameCloud(BirdDAO.arbitrarySQL("select * from bird"));
					
					} catch(Exception e) {};
					
					
					try {
						htmlWindow rW = new htmlWindow(htmlWindow.fileToString(gen));
						rW.renderHtml();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
			};
		}
		return generate;
	}

}
